{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-5e1a66207665>:4: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "# 载入数据\n",
    "mnist = input_data.read_data_sets('MNIST_data', one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-2-df0e5d11a7aa>:31: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See @{tf.nn.softmax_cross_entropy_with_logits_v2}.\n",
      "\n",
      "Iter 0 Testing Accuracy: 0.9577,  lr: 1.0\n",
      "Iter 1 Testing Accuracy: 0.969,  lr: 0.98\n",
      "Iter 2 Testing Accuracy: 0.9744,  lr: 0.9604\n",
      "Iter 3 Testing Accuracy: 0.9759,  lr: 0.941192\n",
      "Iter 4 Testing Accuracy: 0.9767,  lr: 0.92236817\n",
      "Iter 5 Testing Accuracy: 0.9797,  lr: 0.90392077\n",
      "Iter 6 Testing Accuracy: 0.981,  lr: 0.8858424\n",
      "Iter 7 Testing Accuracy: 0.9815,  lr: 0.86812556\n",
      "Iter 8 Testing Accuracy: 0.9817,  lr: 0.850763\n",
      "Iter 9 Testing Accuracy: 0.9822,  lr: 0.83374774\n",
      "Iter 10 Testing Accuracy: 0.982,  lr: 0.8170728\n",
      "Iter 11 Testing Accuracy: 0.9822,  lr: 0.80073136\n",
      "Iter 12 Testing Accuracy: 0.9823,  lr: 0.7847167\n",
      "Iter 13 Testing Accuracy: 0.9829,  lr: 0.7690224\n",
      "Iter 14 Testing Accuracy: 0.9824,  lr: 0.75364196\n",
      "Iter 15 Testing Accuracy: 0.9831,  lr: 0.7385691\n",
      "Iter 16 Testing Accuracy: 0.983,  lr: 0.72379774\n",
      "Iter 17 Testing Accuracy: 0.9836,  lr: 0.70932174\n",
      "Iter 18 Testing Accuracy: 0.9839,  lr: 0.69513535\n",
      "Iter 19 Testing Accuracy: 0.9838,  lr: 0.68123263\n",
      "Iter 20 Testing Accuracy: 0.9836,  lr: 0.66760796\n",
      "Iter 21 Testing Accuracy: 0.9833,  lr: 0.6542558\n",
      "Iter 22 Testing Accuracy: 0.9846,  lr: 0.6411707\n",
      "Iter 23 Testing Accuracy: 0.9831,  lr: 0.6283473\n",
      "Iter 24 Testing Accuracy: 0.984,  lr: 0.61578035\n",
      "Iter 25 Testing Accuracy: 0.9835,  lr: 0.6034647\n",
      "Iter 26 Testing Accuracy: 0.9837,  lr: 0.59139544\n",
      "Iter 27 Testing Accuracy: 0.9837,  lr: 0.57956755\n",
      "Iter 28 Testing Accuracy: 0.9837,  lr: 0.5679762\n",
      "Iter 29 Testing Accuracy: 0.9837,  lr: 0.55661666\n",
      "Iter 30 Testing Accuracy: 0.9835,  lr: 0.5454843\n",
      "Iter 31 Testing Accuracy: 0.984,  lr: 0.5345746\n",
      "Iter 32 Testing Accuracy: 0.9844,  lr: 0.52388316\n",
      "Iter 33 Testing Accuracy: 0.9846,  lr: 0.5134055\n",
      "Iter 34 Testing Accuracy: 0.9839,  lr: 0.50313735\n",
      "Iter 35 Testing Accuracy: 0.9845,  lr: 0.49307463\n",
      "Iter 36 Testing Accuracy: 0.9847,  lr: 0.48321313\n",
      "Iter 37 Testing Accuracy: 0.9846,  lr: 0.47354886\n",
      "Iter 38 Testing Accuracy: 0.9846,  lr: 0.4640779\n",
      "Iter 39 Testing Accuracy: 0.9846,  lr: 0.45479634\n",
      "Iter 40 Testing Accuracy: 0.9848,  lr: 0.4457004\n",
      "Iter 41 Testing Accuracy: 0.9843,  lr: 0.43678638\n",
      "Iter 42 Testing Accuracy: 0.9837,  lr: 0.42805067\n",
      "Iter 43 Testing Accuracy: 0.9841,  lr: 0.41948965\n",
      "Iter 44 Testing Accuracy: 0.9841,  lr: 0.41109985\n",
      "Iter 45 Testing Accuracy: 0.9839,  lr: 0.40287787\n",
      "Iter 46 Testing Accuracy: 0.984,  lr: 0.3948203\n",
      "Iter 47 Testing Accuracy: 0.984,  lr: 0.3869239\n",
      "Iter 48 Testing Accuracy: 0.9836,  lr: 0.3791854\n",
      "Iter 49 Testing Accuracy: 0.9838,  lr: 0.3716017\n"
     ]
    }
   ],
   "source": [
    "# 批次的大小\n",
    "batch_size = 128\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None,784])\n",
    "y = tf.placeholder(tf.float32, [None, 10])\n",
    "keep_prob = tf.placeholder(tf.float32)\n",
    "lr = tf.Variable(1,dtype = tf.float32)\n",
    "\n",
    "# 创建神经网络\n",
    "W1 = tf.Variable(tf.truncated_normal([784,2000],stddev=0.1))\n",
    "b1 = tf.Variable(tf.zeros([1, 2000]))\n",
    "# 激活层\n",
    "layer1 = tf.nn.relu(tf.matmul(x,W1) + b1)\n",
    "# drop层\n",
    "layer1 = tf.nn.dropout(layer1,keep_prob=keep_prob)\n",
    "\n",
    "# 第二层\n",
    "W2 = tf.Variable(tf.truncated_normal([2000,500],stddev=0.1))\n",
    "b2 = tf.Variable(tf.zeros([1, 500]))\n",
    "layer2 = tf.nn.relu(tf.matmul(layer1,W2) + b2)\n",
    "layer2 = tf.nn.dropout(layer2,keep_prob=keep_prob)\n",
    "\n",
    "# 第三层\n",
    "W3 = tf.Variable(tf.truncated_normal([500,10],stddev=0.1))\n",
    "b3 = tf.Variable(tf.zeros([1,10]))\n",
    "# prediction = tf.nn.softmax(tf.matmul(layer2,W3) + b3)\n",
    "prediction = tf.matmul(layer2,W3) + b3\n",
    "\n",
    "\n",
    "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))\n",
    "\n",
    "# 梯度下降法\n",
    "# train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "train_step = tf.train.AdadeltaOptimizer(lr).minimize(loss)  \n",
    "# train_step = tf.train.AdamOptimizer().minimize(loss)  \n",
    "# train_step = tf.train.RMSPropOptimizer(learning_rate=0.001).minimize(loss) \n",
    "# train_step = tf.train.AdagradOptimizer(learning_rate=1).minimize(loss) \n",
    "\n",
    "\n",
    "# 初始化变量\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "prediction_2 = tf.nn.softmax(prediction)\n",
    "# 得到一个布尔型列表，存放结果是否正确\n",
    "correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction_2,1)) #argmax 返回一维张量中最大值索引\n",
    "\n",
    "# 求准确率\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) # 把布尔值转换为浮点型求平均数\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for epoch in range(50):\n",
    "        sess.run(tf.assign(lr, 1*(0.98**epoch)))\n",
    "        for batch in range(n_batch):\n",
    "            # 获得批次数据\n",
    "            batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step, feed_dict={x:batch_xs, y:batch_ys, keep_prob:0.8})\n",
    "        learning_rate = sess.run(lr)\n",
    "        acc = sess.run(accuracy, feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0} )\n",
    "        print(\"Iter \" + str(epoch) + \" Testing Accuracy: \" + str(acc)+ \",  lr: \"+ str(learning_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
